/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.bme.j2ee.v37zen.entities;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 *
 * @author Kiss Dániel
 */
@Entity
@Table(name = "SD_DIAL")
@NamedQueries({
    @NamedQuery(name="Dial.findDialByID", query ="SELECT d FROM Dial d WHERE d.id = :id")
})
public class Dial implements Serializable {
    private static final long serialVersionUID = 1003985371970993341L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String url;
    private String title;
    private String file;    
    @Column(name="DIAL_ORDER")
    private int order;
    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    private History history;    
    /**
     * For future usage.
     */
    private String category;
    @ElementCollection
    @CollectionTable(name="SD_DIAL_TAGS")
    private Set<String> tags;

    public Dial() {
        this.history = new History(new Date());
    }

    public Dial(String url, String title, String file, int order, String category) {
        this(url, title, file, order, category, null);
        this.tags = new HashSet<String>();
    }
    
    public Dial(String url, String title, String file, int order, String category, Set<String> tags) {
        this(url, title, file, order, category, tags, 
                new History(new Date())
             );        
    }

    public Dial(String url, String title, String file, int order, String category, Set<String> tags, History history) {
        this.url = url;
        this.file = file;
        this.title = title;
        this.order = order;
        this.history = history;
        this.category = category;
        this.tags = tags;
    }   
    

   
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }      

    /**
     * Get the value of url
     *
     * @return the value of url
     */
    public URL getUrl() {
        URL urlUrl =null;
        try {
            urlUrl = new URL(url);
        } catch (MalformedURLException ex) {
            Logger.getLogger(Dial.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return urlUrl;
    }

    /**
     * Set the value of url
     *
     * @param url new value of url
     */
    public void setUrl(URL url) {
        this.url = url.toString();
    }

    public String getFile() {
        return file;
    }

    public void setFile(String file) {
        this.file = file;
    }    
    
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }
    
    public History getHistory() {
        return history;
    }

    public void setHistory(History history) {
        this.history = history;
    }
    
    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Set<String> getTags() {
        return tags;
    }

    public void setTags(Set<String> tags) {
        this.tags = tags;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 89 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 89 * hash + (this.url != null ? this.url.hashCode() : 0);
        hash = 89 * hash + (this.title != null ? this.title.hashCode() : 0);
        hash = 89 * hash + (this.file != null ? this.file.hashCode() : 0);
        hash = 89 * hash + this.order;
        hash = 89 * hash + (this.history != null ? this.history.hashCode() : 0);
        hash = 89 * hash + (this.category != null ? this.category.hashCode() : 0);
        hash = 89 * hash + (this.tags != null ? this.tags.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Dial)) {
            return false;
        }
        Dial other = (Dial) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Dial{" + "id=" + id + ", url=" + url + ", title=" + title + ", file=" + file + ", order=" + order + ", history=" + history + ", category=" + category + ", tags=" + tags + '}';
    }

    
    
    
}
